import { cloneDeep } from 'lodash';
import { defineStore } from 'pinia';
import { ref } from 'vue';

const state = ref({
  loginFlag: false,
  userInfo: {
    name: '',
    id: '',
    password: '',
    state: 0,
  },
  //当前选中的菜单
  currentMenu: null,
  //监控登录信息
  videoData: {
    ip: '',
    port: '',
    userName: '',
    password: '',
    channelNo: '',
    videoLogin: false,
  },
  channels: [],
  webCtrlMap: new Map<String, Object>(),
});

export const userSystemStore = defineStore('system', {
  state: () => {
    return state;
  },
  actions: {
    login(userInfo) {
      this.loginFlag = true;
      this.userInfo = userInfo;
      localStorage.setItem('userInfo', JSON.stringify(userInfo));
    },
    getUserInfo() {
      return this.userInfo;
    },
    setCurrentMenu(menuName) {
      this.currentMenu = menuName;
    },
    getCurrentMenu() {
      return this.currentMenu;
    },
    setVideoData(data) {
      this.videoData = data;
    },
    getVideoData() {
      return this.videoData;
    },
    logout() {
      this.$reset();
      this.loginFlag = false;
    },
    setChannels(channelData) {
      this.channels = channelData;
    },
    getChannels() {
      return this.channels;
    },
    getWebCtrlMapByName(name) {
      return this.webCtrlMap.get(name);
    },
    setWebCtrlMapByName(name, webCtrlObje) {
      let webCtrlObjeCopy = cloneDeep(webCtrlObje['']);
      return this.webCtrlMap.set(name, webCtrlObjeCopy);
    },
    removeWebCtrlByName(name) {
      this.webCtrlMap.remove(name);
    },
  },
});
